/* 
	PROJECT: scsu-csc653-chatter
	ABOUT: handles sending output to the chatter server using varous methods that format string data
	DATE: 09-2010
	AUTHORS: skenny214@gmail.com, jinhongli08@gmail.com, hourigan.brian@gmail.com, yaqiluo2010@gmail.com
*/

package chatter.client;

import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class ChatterClientOutputStream {

	OutputStream output;
	DataOutput doutput;
	
	//construct new object with a preconstructed print stream that we will use for writing
	public ChatterClientOutputStream(OutputStream output){
		this.output = output;
		this.doutput = new DataOutputStream(this.output);
	}
	
	//send messsage to chatter server to log the user in
	public void login(String username, String password){
		if(ChatterClientApplet.isDebug) System.out.println("Logging in: "+username+", with password: "+password);
		
		if(username != "" && password != ""){
			try {
				doutput.writeUTF("%%Operation=login%%"+username+"%%"+password+"%%superuser=false");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}
	
	
	//send message to chater server to log the user out
	public void logout(String username){
		if(ChatterClientApplet.isDebug) System.out.println("Logging out: "+username);
		
		if(username != ""){
			try {
				doutput.writeUTF("%%Operation=logout%%"+username);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}
	
	//construct and send a user message that will be set to the server
	public void sendMessage(String username, String message){
		if(ChatterClientApplet.isDebug) System.out.println("Sending message: "+message+", for user: "+username);
		
		if(username != "" && message != ""){
			try {
				doutput.writeUTF("%%Operation=sendmessage%%"+username+"%%"+message+"");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}
}
